package com.cheboohui.iqs.deal;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.alibaba.fastjson.JSONObject;
import com.cheboohui.iqs.config.Config;
import com.cheboohui.iqs.menu.ErrorNews;
import com.cheboohui.iqs.spring.entity.Policy;
import com.cheboohui.iqs.spring.entity.price.request.PolicyRequest;
import com.cheboohui.iqs.spring.entity.price.request.PriviousRequest;
import com.cheboohui.iqs.spring.entity.price.response.ConfirmInsuredResult;
import com.cheboohui.iqs.spring.entity.price.response.PriviousResult;
import com.cheboohui.iqs.taiping.direct.ResTaiPing;
import com.cheboohui.iqs.taiping.direct.e.E09Request;

/**
 * 查询接口相关处理
 * 
 * @author xielonghai
 *
 */
public class CheckDeal {
	/**
	 * logger:日志工具.
	 */
	private static Log log = LogFactory.getLog(CheckDeal.class);

	/**
	 * 往年投保单查询处理方法
	 * 
	 * @param priviousRequest
	 * @return
	 */
	public static PriviousResult privious(PriviousRequest priviousRequest) {
		log.info("进入" + Config.value + ",往年投保单查询接口....");
		try {
			PriviousResult result = new PriviousResult(ErrorNews.L03, "暂不提供该接口");
			log.info(Config.value + ",往年投保单查询接口结果:" + JSONObject.toJSONString(result));
			return result;
		} catch (Exception e) {
			log.error(Config.value + ",往年投保单查询接口实现出现异常:" + e);
			return new PriviousResult(ErrorNews.L03, e.getMessage());
		}
	}

	/**
	 * 保单相关查询
	 * 
	 * @param policyRequest
	 * @param type
	 *            类型：01,投保单查询;02,保单查询
	 * @return
	 */
	public static PriviousResult policy(PolicyRequest policyRequest, String type) {
		log.info("进入" + Config.value + ",保单相关查询接口....");
		try {
			PriviousResult result = new PriviousResult(ErrorNews.L03, "暂不提供该接口");
			log.info(Config.value + ",保单相关查询接口结果:" + JSONObject.toJSONString(result));
			return result;
		} catch (Exception e) {
			log.error(Config.value + ",保单相关查询接口实现出现异常:" + e);
			return new PriviousResult(ErrorNews.L03, e.getMessage());
		}
	}

	/**
	 * 根据据投保单号查询单子状态
	 * 
	 * @param policyRequest
	 * @return
	 */
	public static ConfirmInsuredResult checkOrder(PolicyRequest policyRequest) {
		log.info("进入" + Config.value + ",根据据投保单号查询单子状态....");
		try {
			ConfirmInsuredResult result = null;// new
												// ConfirmInsuredResult(ErrorNews.L03,
												// "暂不提供该接口");
			ResTaiPing e09 = E09Request.requestE09("", policyRequest.getOrderNo());
			if (!e09.isState()) {
				log.error(Config.value + ",根据据投保单号查询单子状态....查询失败:" + e09.getMsg());
				result = new ConfirmInsuredResult(e09.getError(), e09.getMsg());
			} else {
				log.error(Config.value + ",根据据投保单号查询单子状态....查询成功--开始处理....");
				Policy policy = new Policy();
				policy.setOrderNo(policyRequest.getOrderNo());
				policy.setBusinessInsuredNo(policyRequest.getBusinessInsuredNo());
				policy.setTraficInsuredNo(policyRequest.getTraficInsuredNo());

				String status = e09.getResult().getJSONArray("order").getJSONObject(0).getString("status");
				if ("R_T_F".equals(status)) {
					policy.setCode("10");
					policy.setCodeValue("待处理");
					result = new ConfirmInsuredResult(policy);
				} else if ("R_T_P".equals(status)) {
					policy.setCode("07");
					policy.setCodeValue("待支付");
					result = new ConfirmInsuredResult(policy);
				} else if ("U_I".equals(status)) {
					policy.setCode("08");
					policy.setCodeValue("待出单");
					result = new ConfirmInsuredResult(policy);
				} else if ("U_E".equals(status)) {
					policy.setCode("09");
					policy.setCodeValue("已出单");
					result = new ConfirmInsuredResult(policy);
				} else {
					log.info(Config.value + ",根据投保单号查询单子状态....查询成功--开始处理失败,未发现订单状态,太平直连接口出现错误status:" + status);
					result = new ConfirmInsuredResult(ErrorNews.L03, "根据投保单号查询单子状态,未发现订单状态,太平直连接口出现错误status:" + status);
				}
			}
			log.info(Config.value + ",根据据投保单号查询单子状态接口结果:" + JSONObject.toJSONString(result));
			return result;
		} catch (Exception e) {
			log.error(Config.value + ",根据据投保单号查询单子状态接口实现出现异常:" + e);
			return new ConfirmInsuredResult(ErrorNews.L03, e.getMessage());
		}
	}
}
